package com.vmj.heap;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangshoujun on 2017/9/9 下午8:18.
 */
public class StringPool {

    public static final Map<String, VMObject> internalStringsMap = new HashMap<String, VMObject>();

    public static VMObject getStringObject(ClassLoader classLoader, String str) {
        if(internalStringsMap.containsKey(str)) {
            return internalStringsMap.get(str);
        }

        char[] chars = str.toCharArray();
        VMObject obj = new VMObject(classLoader.loadClass("java/lang/String"));
        obj.setData(chars);

        VMObject strObj = classLoader.loadClass("java/lang/String").newObject();
        strObj.setRefVar("value", "[C", obj);
        internalStringsMap.put(str, strObj);
        return strObj;
    }
}
